package setting

import (
	"log"
	"time"

	"github.com/go-ini/ini"
)

type App struct {
	JwtSecret string
	PageSize  int
	PrefixUrl string

	RuntimeRootPath string

	ImageSavePath  string
	ImageMaxSize   int
	ImageAllowExts []string

	ExportSavePath string
	QrCodeSavePath string
	FontSavePath   string

	LogSavePath string
	LogSaveName string
	LogFileExt  string
	TimeFormat  string
}

var AppSetting = &App{}

type Server struct {
	RunMode      string
	HttpPort     int
	ReadTimeout  time.Duration
	WriteTimeout time.Duration
	ClientNode   string
}

var ServerSetting = &Server{}

type Database struct {
	Type        string
	User        string
	Password    string
	Host        string
	Name        string
	TablePrefix string
	Loc         string
}

var DatabaseSetting = &Database{}

type Redis struct {
	Host        string
	Password    string
	MaxIdle     int
	MaxActive   int
	IdleTimeout time.Duration
}

var RedisSetting = &Redis{}

type CloudPlatform struct {
	CoinPrice           float64
	DayRentCoins        float64
	OneCardDayRentCoins float64
}

var CloudPlatformSetting = &CloudPlatform{}

type Mail struct {
	SmtpHost    string
	SmtpPort    string
	FromName    string
	FromAddress string
	AuthCode    string
	Subject     string
}

var MailSetting = &Mail{}

type Phone struct {
	SignName        string
	TemplateCode    string
	AccessKeyID     string
	AccessKeySecret string
}

var PhoneSetting = &Phone{}

type Alipay struct {
	AppID        string
	AlipayPubkey string
	AppPrvkey    string
	ReturnUrl    string
	NotifyUrl    string
	DebugSwitch  int
	Subject      string
}

var AlipaySetting = &Alipay{}

type Paypal struct {
	ClientID    string
	Secret      string
	DebugSwitch int
	ReturnUrl   string
	CancelUrl   string
}

var PaypalSetting = &Paypal{}

var cfg *ini.File

type OpenAi struct {
	AccessKeyId             string
	AccessKeySecret         string
	ResourceOwnerAccount    string
	Laasendpoint            string
	Laastokenport           int
	LaasECSport             int
	Laasnetport             int
	ContainerCloudendpoint  string
	ContainerCloudHttpsport int
	ContainerCloudHttport   int
}

var OpenAiSetting = &OpenAi{}

// Setup initialize the configuration instance
func Setup() {
	var err error
	//cfg, err = ini.Load("conf/app.ini")
	cfg, err = ini.LoadSources(ini.LoadOptions{IgnoreInlineComment: true}, "conf/app.ini")
	if err != nil {
		log.Fatalf("setting.Setup, fail to parse 'conf/app.ini': %v", err)
	}

	mapTo("app", AppSetting)
	mapTo("server", ServerSetting)
	mapTo("database", DatabaseSetting)
	mapTo("redis", RedisSetting)
	mapTo("cloudplatform", CloudPlatformSetting)
	mapTo("mail", MailSetting)
	mapTo("phone", PhoneSetting)
	mapTo("alipay", AlipaySetting)
	mapTo("paypal", PaypalSetting)
	mapTo("openai", OpenAiSetting)

	AppSetting.ImageMaxSize = AppSetting.ImageMaxSize * 1024 * 1024
	ServerSetting.ReadTimeout = ServerSetting.ReadTimeout * time.Second
	ServerSetting.WriteTimeout = ServerSetting.WriteTimeout * time.Second
	RedisSetting.IdleTimeout = RedisSetting.IdleTimeout * time.Second
}

// mapTo map section
func mapTo(section string, v interface{}) {
	err := cfg.Section(section).MapTo(v)
	if err != nil {
		log.Fatalf("Cfg.MapTo %s err: %v", section, err)
	}
}
